import moment from 'dayjs';

export const waitTime = (time = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

export const DatePattern = {
  patternYMD: 'YYYY-MM-DD',
  patternYMDHMS: 'YYYY-MM-DD HH:mm:ss',
  patternYMDHM: 'YYYY-MM-DD HH:mm'
}


// 获取当日时间区间(0点到当前)
export const getCurrentDayDate = () => {
  let startTime = moment().startOf('day').format('YYYY-MM-DD HH:mm:ss')
  let endTime = moment().format('YYYY-MM-DD HH:mm:ss')
  return startTime + ',' + endTime;
}

//判断日期是否为一个正确的日期格式
export const checkDate = (dateStr) => {
  dateStr = dateStr.replace(/\//g, '-');
  let dateReg = /^(\d{4})-(\d{1,2})-(\d{1,2})$/;
  let rValue = dateStr.match(dateReg);
  if (rValue === null) {
    return false;
  }
  rValue[1] = parseInt(rValue[1], 10);
  rValue[2] = parseInt(rValue[2] - 1, 10);
  rValue[3] = parseInt(rValue[3], 10);
  let dateObj = new Date(rValue[1], rValue[2], rValue[3]);
  if (dateObj.getFullYear() !== rValue[1] || dateObj.getMonth() !== rValue[2] || dateObj.getDate() !== rValue[3]) {
    return false;
  }
  return true;
}

//计算相差天数
export const datedifference = (sDate1, sDate2) => {    //sDate1和sDate2是2006-12-18格式
  let dateSpan,
    tempDate,
    iDays;
  sDate1 = Date.parse(sDate1);
  sDate2 = Date.parse(sDate2);
  dateSpan = sDate2 - sDate1;
  dateSpan = Math.abs(dateSpan);
  iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
  let sam = compareDate(sDate1, sDate2)
  return sam ? iDays : -iDays
};

//比较日期的早晚
export const compareDate = (date1, date2) => {
  let oDate1 = new Date(date1);
  let oDate2 = new Date(date2);
  if (oDate1.getTime() >= oDate2.getTime()) {
    return true; //第一个大
  } else {
    return false; //第二个大
  }
}

export const dateFormat = (date, dateFormat) => {
  if (!moment(date).isValid()) return date;
  return moment(date).format(dateFormat === undefined ? DatePattern.patternYMD : dateFormat);
}

export const rangerDateFormat = (startTime, endTime, dateFormat) => {
  if (!moment(startTime).isValid()) return startTime;
  if (!moment(endTime).isValid()) return endTime;
  return `${moment(startTime).format(dateFormat === undefined ? DatePattern.patternYMD : dateFormat)} 至 ${moment(endTime).format(dateFormat === undefined ? formatter : dateFormat)}`;
}

export const formatSeconds = value => {
  let theTime = parseInt(value);// 需要转换的时间秒
  let theTime1 = 0;// 分
  let theTime2 = 0;// 小时
  let theTime3 = 0;// 天
  if (theTime > 60) {
    theTime1 = parseInt(theTime / 60);
    theTime = parseInt(theTime % 60);
    if (theTime1 > 60) {
      theTime2 = parseInt(theTime1 / 60);
      theTime1 = parseInt(theTime1 % 60);
      if (theTime2 > 24) {
        //大于24小时
        theTime3 = parseInt(theTime2 / 24);
        theTime2 = parseInt(theTime2 % 24);
      }
    }
  }
  let result = '';
  if (theTime > 0) {
    result = '' + parseInt(theTime) + '秒';
  }
  if (theTime1 > 0) {
    result = '' + parseInt(theTime1) + '分' + result;
  }
  if (theTime2 > 0) {
    result = '' + parseInt(theTime2) + '小时' + result;
  }
  if (theTime3 > 0) {
    result = '' + parseInt(theTime3) + '天' + result;
  }
  return result;
}

export const formatDate = (date) => {
  let newdate = new Date(date);
  let nowdate = new Date();
  let YY = newdate.getFullYear() + '-';
  let YY2 = nowdate.getFullYear() + '-';
  let MM = (newdate.getMonth() + 1 < 10 ? '0' + (newdate.getMonth() + 1) : newdate.getMonth() + 1) + '-';
  let DD = (newdate.getDate() < 10 ? '0' + (newdate.getDate()) : newdate.getDate());
  let hh = (newdate.getHours() < 10 ? '0' + newdate.getHours() : newdate.getHours()) + ':';
  let mm = (newdate.getMinutes() < 10 ? '0' + newdate.getMinutes() : newdate.getMinutes());
  // let ss = (newdate.getSeconds() < 10 ? '0' + newdate.getSeconds() : newdate.getSeconds());
  let A = newdate.setHours(0, 0, 0, 0);
  let B = nowdate.setHours(0, 0, 0, 0);
  let time
  if (YY === YY2) {
    if (A === B) {
      time = hh + mm;
    } else {
      time = MM + DD + ' ' + hh + mm;
    }
  } else {
    time = YY + MM + DD + ' ' + hh + mm;
  }
  return time
}

export const formatTime = (date) => {
  let newdate = new Date(date);
  let nowdate = new Date();
  let MM = (newdate.getMonth() + 1 < 10 ? '0' + (newdate.getMonth() + 1) : newdate.getMonth() + 1) + '-';
  let DD = (newdate.getDate() < 10 ? '0' + (newdate.getDate()) : newdate.getDate());
  let hh = (newdate.getHours() < 10 ? '0' + newdate.getHours() : newdate.getHours()) + ':';
  let mm = (newdate.getMinutes() < 10 ? '0' + newdate.getMinutes() : newdate.getMinutes());
  let A = newdate.setHours(0, 0, 0, 0);
  let B = nowdate.setHours(0, 0, 0, 0);
  let time
  let obj = {
    '-172,800,000': -2,
    '-86400000': -1,
    0: 0,
  };
  if(obj[A - B]===0){
    time= hh + mm
  }
  else if(obj[A - B]===-1){
    time= '昨天'
  }
  else{
    time= MM+DD
  }
  return time
}

